﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Majoopic.Service.Model;
using Majoopic.Service;
using Majoopic.DataAccess.Model;
using Majoopic.Engine;
using Majoopic.WPFClient.ViewModels;

namespace Majoopic.WPFClient
{
    /// <summary>
    /// General mediator of the application
    /// </summary>
    public class Mediator
    {
        #region Singleton
        private static readonly Mediator _Current;
        /// <summary>
        /// Get the mediator of the appication
        /// </summary>
        public static Mediator Current
        {
            get
            {               
                return _Current;
            }
        }
        static Mediator()
        {
            _Current = new Mediator();
            _Current.Init();
        }
        #endregion

        /// <summary>
        /// Get the current service context
        /// </summary>
        public ServiceContext CurrentServiceContext
        {
            get;
            set;
        }

        private FileEngine _CurrentFileEngine;
        /// <summary>
        /// Current file job running
        /// </summary>
        public FileEngine CurrentFileEngine
        {
            get
            {
                return this._CurrentFileEngine;
            }
            set
            {
                this._CurrentFileEngine = value;
                if (value != null)
                {
                    this.RaiseEvent(EventNames.FileEngineJobCreated);
                }
            }
        }

        /// <summary>
        /// ViewModel of UCEngineManager
        /// </summary>
        public EngineManagerViewModel EngineManagerViewModel
        {
            get;
            set;
        }

        /// <summary>
        /// ViewModel of ImagesViewer
        /// </summary>
        public ImagesViewerViewModel ImageViewerViewModel
        {
            get;
            set;
        }

        private Dictionary<string, List<Action>> _eventsDictionnary = new Dictionary<string,List<Action>>();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="eventName"></param>
        /// <param name="action"></param>
        public void SubscribeToEvent(string eventName, Action action)
        {
            List<Action> actions;
            if (this._eventsDictionnary.TryGetValue(eventName, out actions) == false)
            {
                actions = new List<Action>();
                this._eventsDictionnary[eventName] = actions;
            }
            actions.Add(action);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="eventName"></param>
        public void RaiseEvent(string eventName)
        {
            List<Action> actions;
            if (this._eventsDictionnary.TryGetValue(eventName, out actions))
            {
                actions.ForEach(x => x());
            }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public Mediator()
        {
            
        }

        private void Init()
        {
            this.InitServiceContext();
            this.InitViewModel();
        }

        private void InitViewModel()
        {
            this.EngineManagerViewModel = new EngineManagerViewModel();
            this.ImageViewerViewModel = new ImagesViewerViewModel();
        }

        private void InitServiceContext()
        {
            this.CurrentServiceContext = this.CreateServiceContext();
        }

        private ServiceContext CreateServiceContext()
        {
            // Hard init of the device and the test factory during the development process
            // TODO : Connect the application to the database with the correct service and launch on the startup
            // the UCDeviceRegister to select the current device or read it from a config file
            ServiceContext serviceContext = new ServiceContext();
            serviceContext.CurrentDevice = new Device();
            serviceContext.CurrentDevice.DeviceName = "DeviceTest";
            serviceContext.CurrentFactory = ServiceAbstractFactory.CreateFactory(ServiceFactoryType.Test);
            return serviceContext;
        }
    }
}
